package ru.timofeyco.justfeelbetter.adapters;

import java.util.ArrayList;
import java.util.HashMap;

import ru.timofeyco.justfeelbetter.EditTypesActivity;
import ru.timofeyco.justfeelbetter.R;
import ru.timofeyco.justfeelbetter.model.DrillType;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.TextView;

public class TypesListAdapter extends ArrayAdapter<DrillType> {
	private ArrayList<DrillType> items;
	private HashMap<View, DrillType> textViewToDrillTypeMap;
	private HashMap<View, DrillType> rateViewToDrillTypeMap;
	//private HashMap<View, DrillType> colorViewToDrillTypeMap;
	private Context context;
	
	/**
	 * @param context
	 * @param textViewResourceId
	 * @param objects
	 */
	public TypesListAdapter(Context context, int textViewResourceId,
			ArrayList<DrillType> objects) {
		super(context, textViewResourceId, objects);
		this.items = objects;			
		this.context = context;
		
		// helpers for drilltype access by element
		this.textViewToDrillTypeMap = new HashMap<View, DrillType>();
		this.rateViewToDrillTypeMap = new HashMap<View, DrillType>();
		//this.colorViewToDrillTypeMap = new HashMap<View, DrillType>();
	}
	
	 @Override
	 public View getView(int position, View convertView, ViewGroup parent) {
	     View view = convertView;
	     if (view == null) {
	         LayoutInflater layoutInflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	    	 view = layoutInflater.inflate(R.layout.types_row, null);
	     }
	     DrillType drillType = items.get(position);
	     if (drillType != null) {
	         TextView rowTextView = (TextView) view.findViewById(R.id.typeListRowText);             
	         if (rowTextView != null) {            	 
	        	 rowTextView.setText(drillType.getName());  
	        	 //rowTextView.setOnClickListener(typeNameClickListener);
		         if (!textViewToDrillTypeMap.containsKey(rowTextView)) {
		        	 this.textViewToDrillTypeMap.put(rowTextView, drillType);
		         }	
	         }             
	         	         
	         EditText countEditText = (EditText) view.findViewById(R.id.typeCountListRowText);
	         if (countEditText != null) {            	 
	        	 countEditText.setText(Integer.toString(drillType.getRate()));
	        	 if (!rateViewToDrillTypeMap.containsKey(countEditText)) {
		        	 this.rateViewToDrillTypeMap.put(countEditText, drillType);
		         }	
	        	 
	        	 //countEditText.setOnClickListener(typeRateClickListener);
	        	 
		         countEditText.setOnLongClickListener(new OnLongClickListener() {
					@Override
					public boolean onLongClick(View v) {
						DrillType gottenDrillType = TypesListAdapter.this.rateViewToDrillTypeMap.get(v);
						
						EditTypesActivity editTypesActivity = (EditTypesActivity)(TypesListAdapter.this.context);
						editTypesActivity.showRateEditingDialog(gottenDrillType, (EditText) v);
						return false;
					}
				});
	         }
	         
	         /*
	         FrameLayout frameLayout = (FrameLayout) view.findViewById(R.id.typeColorRowView);
	         if (frameLayout != null) {            	 
	        	 frameLayout.setBackgroundColor(drillType.getColor()); 
	        	 //frameLayout.setOnClickListener(typeColorClickListener);
	        	 
	        	 if (!colorViewToDrillTypeMap.containsKey(frameLayout)) {
		        	 this.colorViewToDrillTypeMap.put(frameLayout, drillType);
		         }	
	         }
	         */
	     }
	     return view;
	 }	 
	 
	 /*
	private OnClickListener typeNameClickListener = new OnClickListener() {				
		@Override
		public void onClick(View v) {			
			TextView tv = (TextView) v;
			Toast.makeText(context, "Type text: " + tv.getText(), Toast.LENGTH_LONG).show();
		}
	};
	
	private OnClickListener typeRateClickListener = new OnClickListener() {				
		@Override
		public void onClick(View v) {
			EditText etv = (EditText) v;
			Toast.makeText(context, "Type rate text: " + etv.getText(), Toast.LENGTH_LONG).show();
		}
	};
	
	private OnClickListener typeColorClickListener = new OnClickListener() {				
		@Override
		public void onClick(View v) {
			FrameLayout colorL = (FrameLayout) v;
			Toast.makeText(context, "Type color: " + colorL.getBackground(), Toast.LENGTH_LONG).show();
		}
	};
	*/
}
